package model;

import parsing.RaceResultParsingInformation;
import utility.Utility;

public class Runner {
	private int id = -1 ; // Only exists if the runner is in the database.
	private String name ; 
	private boolean isMale ; 
	private String town ; 
	private String state ; 
	private int birthYear ;
	
	public Runner(String dataString, RaceResultParsingInformation parsingInformation) {
		this.name = dataString.substring(parsingInformation.getNameStart(), parsingInformation.getNameEnd()).trim() ;
		this.isMale = "M".equals(dataString.substring(parsingInformation.getSexStart(), parsingInformation.getSexEnd())) ;
		this.state = dataString.substring(parsingInformation.getStateStart(), parsingInformation.getStateEnd()) ; 
		this.town = dataString.substring(parsingInformation.getTownStart(), parsingInformation.getTownEnd()).trim() ;
		int age = Utility.getInt(dataString.substring(parsingInformation.getAgeStart(), parsingInformation.getAgeEnd()).trim()) ;
		this.birthYear = parsingInformation.getYearOfRace() - age ; 
	}
	
	public Runner(int id, String name, String town, String stateCode, int birthYear, boolean isMale) {
		this.id = id ; 
		this.name = name ; 
		this.town = town ; 
		this.state = stateCode ; 
		this.birthYear = birthYear ; 
		this.isMale = isMale ; 
	}
	
	public String getName() {
		return name;
	}
	public boolean isMale() {
		return isMale;
	}
	public String getTown() {
		return town;
	}
	public String getState() {
		return state;
	}
	public int getBirthyear() {
		return birthYear;
	}

	public int getId() {
		return this.id ; 
	}
	
	@Override
	public String toString() {
		return "name = " + name + ", birthyear=" + birthYear + ", isMale=" + isMale
				+ ", state=" + state + ", town=" + town
				+ "]";
	}

	public int getAge(int currentYear) {
		return currentYear - this.birthYear ;
	}

	/**
	 * Returns a string that is based on those properties we want to use to define a unique person.
	 * @return
	 */
	public String getKey() {
		String key = this.name + "_" + this.birthYear + "_" +  this.isMale ;
		return key ; 
	}
	
	public String getCohortKey(int currentYear) {
		String key = "F" ; 
		if(this.isMale()) {
			key = "M" ; 
		}
		key += "_" + this.getAge(currentYear) ;
		return key ; 
	}
	
	@Override
	public int hashCode() {
		final int prime = 31 ;
		int result = 1 ;
		result = prime * result + this.birthYear ;
		result = prime * result + (this.isMale ? 1231 : 1237) ;
		result = prime * result + ((this.name == null) ? 0 : this.name.hashCode()) ;
		return result ;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true ;
		if (obj == null)
			return false ;
		if (getClass() != obj.getClass())
			return false ;
		Runner other = (Runner) obj ;
		if (this.birthYear != other.birthYear)
			return false ;
		if (this.isMale != other.isMale)
			return false ;
		if (this.name == null) {
			if (other.name != null)
				return false ;
		}
		else if (!this.name.equals(other.name))
			return false ;
		return true ;
	}

	public void setId(int personId) {
		this.id = personId ; 
	}
	
}
